<template>
  <div class="app-container">
    <cus-wraper>
      <cus-filter-wraper>
        <div>
          <el-input v-model="listQuery.id" onkeyup="value=value.replace(/[^\d]/g,'')" placeholder="请输入分组Id" style="width:200px;margin: 0 10px 0 0;" clearable></el-input>
          <el-select filterable v-model="listQuery.groupSwitch" class="filter-item" placeholder='请选择是否启用' style="width: 200px;margin: 0 10px 0 0;">
            <el-option v-for="item in flagList2" :key="item.key" :label="item.lab" :value="item.key"></el-option>
          </el-select>
          <el-button type="primary" @click="getList" icon="el-icon-search">查询</el-button>
          <el-button type="info" @click="reGetList" icon="el-icon-search">重置</el-button>
        </div>
        <el-button type="primary" @click="handleCreate" icon="el-icon-search">创建</el-button></cus-filter-wraper>
      <div class="table-container">
        <el-table v-loading="listLoading" :data="list" ref="itsmDataTable" size="mini" fit element-loading-text="Loading"
                  highlight-current-row align="center">
<!--          <el-table-column type="selection" width="45"></el-table-column>-->
          <el-table-column label="分组Id" prop="id" align="center"></el-table-column>
          <el-table-column label="名称" prop="groupName" align="center"></el-table-column>
          <el-table-column label="图片" prop="groupPicture" align="center">
            <template slot-scope="scope">
              <span>
                <img :src="scope.row.groupPicture" alt="" style="max-width: 150px;max-height: 50px">
              </span>
            </template>
          </el-table-column>
          <el-table-column label="是否启用" prop="groupSwitch" align="center" >
            <template slot-scope="scope">
              <el-button type="success" round v-if="scope.row.groupSwitch === 1">已启用</el-button>
              <el-button type="danger" round v-else>已禁用</el-button>
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="groupOrder" align="center" ></el-table-column>
            <el-table-column align="center" v-if="this.global_checkBtnPermission(['sys:user:edit','sys:user:delete'])" label="操作">
            <template slot-scope="scope">
              <el-button  size="mini" type="primary" @click="handleUpdate(scope.row)" icon="el-icon-edit" plain>
                编辑
              </el-button>
              <cus-del-btn  @ok="handleDelete(scope.row)"/>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList"/>
      </div>

      <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogVisible" width="40%" v-dialogDrag @close="handleDialogClose">
        <el-form ref="dataForm" :model="form" :rules="rules" label-width="120px" class="demo-ruleForm">
          <el-form-item label="名称:" prop="username">
            <el-input v-model="form.groupName"></el-input>
          </el-form-item>
          <el-form-item label="图片:" prop="username">
            <el-upload
              action=""
              ref="upload"
              :http-request="Upload"
              :auto-upload="true"
              list-type="picture-card"
              :on-change="handleLimit"
              :on-remove="handleRemove"
              :limit="1"
              :class="{disabled:uploadDisabled}"
              :style="{display:form.groupPicture?'none':'block'}"
            >
              <i class="el-icon-plus" />
            </el-upload>
            <img class="uploadImg" :src="form.groupPicture" :style="{display:form.groupPicture?'block':'none',maxWidth: '150px', maxHeight: '150px'}" alt="">
            <el-button type="primary" @click="reload(1)" class="reload" :style="{display:form.groupPicture?'block':'none'}">重新上传</el-button>
          </el-form-item>

          <el-form-item label="开关:" prop="groupSwitch">
<!--            {{form.groupSwitch}}-->
<!--            {{form.groupOrder}}-->
            <el-select filterable v-model="form.groupSwitch" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序:" prop="groupOrder">
            <el-input-number v-model="form.groupOrder" @change="handleChange" :min="1" :max="1000" label="排序"></el-input-number>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false"> 取 消 </el-button>
          <el-button type="primary" @click="submitForm"> 确 认 </el-button>
        </span>
      </el-dialog>
    </cus-wraper>
  </div>
</template>

<script>
  import { getSysUserPage, addSkillGroup, deleteSysUser } from '@/api/qualification/qualification'
  import { client } from '@/utils/ali-oss'

  export default {
    data() {
      return {
        dialogVisible: false,
        list: [],
        listLoading: true,
        total: 0,
        listQuery: {
          id: undefined,
          page: 1,
          pageSize: 10,
          username: undefined
        },
        flagList: [{ key: '1', lab: 'id' }, { key: '4', lab: '审核状态' }],
        flagList2: [{ key: undefined, lab: '全部状态' },{ key: '1', lab: '启用' }, { key: '2', lab: '禁用' } ],
        flagIndex:'1',
        flagText:'',
        flagText2:undefined,
        form: {
          id: undefined, //主键ID
          groupName: undefined, //账号
          groupPicture: undefined, //账号
          groupOrder: 1, //账号
          groupSwitch: 1, //账号
        },
        dialogStatus: '',
        titleMap: {
          update: '编辑分组',
          create: '创建分组'
        },

        dialogVisibleImg1: false,
        uploadDisabled:false,
        options: [{
          value: 1,
          label: '启用'
        }, {
          value: 2,
          label: '禁用'
        }],
        changeValue: 1,
        searchVal: '',

        rules: {
          groupName: [
            {required: true, message: '请输入表情名称', trigger: 'blur'},
          ],
          groupPicture: [
            {required: true, message: '请上传表情图片', trigger: 'blur'},
          ],
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.listLoading = true;
        getSysUserPage(this.listQuery).then(response => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.listLoading = false;
        })
      },
      reGetList(){
        this.listQuery.id = undefined
        this.listQuery.page = 1
        this.listQuery.pageSize = 10
        this.listQuery.username = undefined
        this.getList()
      },
      handleCreate() {
        this.resetForm()
        this.dialogStatus = 'create';
        this.dialogVisible = true
      },
      handleUpdate(row) {
        // row)
        this.form = Object.assign({}, row);
        this.dialogStatus = 'update';
        this.dialogVisible = true
      },
      handleDelete(row) {
        let id = row.id
        deleteSysUser(id).then(response => {
          if (response.code === 0) {
            this.getList();
            this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });
          }
        })
      },
      submitForm() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            addSkillGroup(this.form).then(response => {
              if (response.code === 0) {
                this.getList();
                this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });;
                this.dialogVisible = false
              } else {
                this.$message({
                message: response.message,
                type: 'error'
            });
              }
            }).catch(err => {

            })
          }
        })
      },
      resetForm() {
        this.form = {
          id: undefined, //主键ID
          groupName: undefined, //账号
          groupPicture: undefined, //账号
          groupOrder: 1, //账号
          groupSwitch: 1, //账号
        }
        if(this.$refs.upload){
          this.$refs.upload.clearFiles();
        }
        if(this.$refs.upload2){
          this.$refs.upload2.clearFiles();
        }
        this.uploadDisabled = false;
        this.uploadDisabled2 = false;
      },
      // 监听dialog关闭时的处理事件
      handleDialogClose() {
        this.resetForm();

        if (this.$refs['dataForm']) {
          this.$refs['dataForm'].clearValidate() // 清除整个表单的校验
        }
      },
      toggleSelection(){
        // 一键删除
        const _selectData = this.$refs.itsmDataTable.selection;
        if(_selectData.length === 0){return}
        let selectArry = [];
        _selectData.map(row => {
          selectArry.push(row.id)
        });

        deleteSysUser(selectArry).then(response => {
          if (response.code === 0) {
            this.getList();
            this.$notify({
                    type:'success',
                    message: '操作成功',
                    position: 'bottom-right',
                    showClose: false,
                    duration: 3000
                });
          }
        })
      },

      // 上传图片
      Upload(file) {
        const that = this;
        const names = ['image/jpg', 'image/jpeg', 'image/png', 'image/gif']
        if (!names.includes(file.file.type)) {this.$message.error('不支持的格式!'); return }
        const fileName = Date.now() + '-' + file.file.name;
        client().put(fileName, file.file,).then(result => {
          that.form.groupPicture = result.url
        }).catch(err => {

        })
      },
      // 删除图片
      handleRemove(fileList) {
        this.form.groupPicture = '';
        this.uploadDisabled = fileList.length>=1
      },
      handleLimit(file,fileList){
        this.uploadDisabled = fileList.length>=1
      },

      reload(val){
        this.form.groupPicture = '';
        this.uploadDisabled = false;
        this.$refs.upload.clearFiles();
      },
      handleChange(value) {

      }

    }
  }
</script>
<style lang="scss" scoped>
  // .el-upload--picture-card 控制加号部分
  .disabled /deep/ .el-upload--picture-card {
    display: none;
  }
  .disabled2 /deep/ .el-upload--picture-card {
    display: none;
  }
  .uploadImg, .uploadImg2{

    border-radius: 6px;
  }
  .reload{
    position: absolute;
    bottom: 0;
    left: 160px;
  }
  .search-line{
    margin-bottom: 10px;
    padding: 10px;
    background: white;
  }
  .filter-wraper{
    display: flex;
    justify-content: space-between;
  }
  .table-container /deep/ .el-table .cell{
    display:flex;
  }
</style>
